package com.mindskip.xzs.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.domain.DepartmentUser;
import com.mindskip.xzs.domain.ExamPaperAnswer;
import com.mindskip.xzs.repository.DepartmentMapper;
import com.mindskip.xzs.repository.DepartmentUserMapper;
import com.mindskip.xzs.repository.NewExamPaperAnswerMapper;
import com.mindskip.xzs.service.NewExamPaperAnswerService;
import com.mindskip.xzs.utility.ExamUtil;
import com.mindskip.xzs.utility.excel.EasyExcelUtil;
import com.mindskip.xzs.viewmodel.admin.exam.FinishPagerRes;
import com.mindskip.xzs.viewmodel.admin.exam.PageFinishPagerReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NewExamPaperAnswerServiceImpl extends ServiceImpl<NewExamPaperAnswerMapper, ExamPaperAnswer> implements NewExamPaperAnswerService {

    @Resource
    private DepartmentUserMapper departmentUserMapper;
    @Resource
    private DepartmentMapper departmentMapper;

    @Override
    public PageInfo<FinishPagerRes> adminPage(PageFinishPagerReq pageFinishPagerReq) {
        // 分页参数
        if (pageFinishPagerReq == null) {
            pageFinishPagerReq = new PageFinishPagerReq()
                    .setPageNum(1)
                    .setPageSize(10);
        } else {
            if (pageFinishPagerReq.getPageNum() == null) {
                pageFinishPagerReq.setPageNum(1);
            }
            if (pageFinishPagerReq.getPageSize() == null) {
                pageFinishPagerReq.setPageSize(10);
            }
        }

        // 分数参数
        if (StringUtils.isNotBlank(pageFinishPagerReq.getBeginScoreVM())) {
            pageFinishPagerReq.setBeginScore(ExamUtil.scoreFromVM(pageFinishPagerReq.getBeginScoreVM()));
        }
        if (StringUtils.isNotBlank(pageFinishPagerReq.getEndScoreVM())) {
            pageFinishPagerReq.setEndScore(ExamUtil.scoreFromVM(pageFinishPagerReq.getEndScoreVM()));
        }
        // 员工参数
        if (pageFinishPagerReq.getDeptId() != null) {
            List<Long> childDeptList = departmentMapper.getAllChildDepts(pageFinishPagerReq.getDeptId());
            List<DepartmentUser> departmentUsers = departmentUserMapper.selectList(
                    new LambdaQueryWrapper<DepartmentUser>()
                            .in(DepartmentUser::getDeptId, childDeptList)
            );
            if (CollectionUtil.isNotEmpty(departmentUsers)) {
                pageFinishPagerReq.setUserQywxIds(departmentUsers.stream().map(DepartmentUser::getUserQywxId).distinct().collect(Collectors.toList()));
            }
        }
        // 是否已完成参数
        if (pageFinishPagerReq.getDone()) {
            pageFinishPagerReq.setStatus(2);
        } else {
            pageFinishPagerReq.setStatus(1);
        }

        PageHelper.startPage(pageFinishPagerReq.getPageNum(), pageFinishPagerReq.getPageSize(), "a.id desc");
        List<FinishPagerRes> finishPagerResList = baseMapper.selectAnswerPaperList(pageFinishPagerReq);
        if (CollectionUtil.isEmpty(finishPagerResList)) {
            return new PageInfo<>();
        }

        // 分数处理
        finishPagerResList.forEach(res -> {
            res.setSystemScore(ExamUtil.scoreToVM(res.getSystemScoreInt()));
            res.setUserScore(ExamUtil.scoreToVM(res.getUserScoreInt()));
            res.setPaperScore(ExamUtil.scoreToVM(res.getPaperScoreInt()));
            res.setDoTime(ExamUtil.changeTimeFormat(res.getDoTimeSec()));
        });
        return new PageInfo<>(finishPagerResList);
    }

    @Override
    public void exportExcel(PageFinishPagerReq pageFinishPagerReq, HttpServletResponse response) {
        PageInfo<FinishPagerRes> finishPagerResPageInfo = adminPage(pageFinishPagerReq);
        List<FinishPagerRes> list = finishPagerResPageInfo.getList();
        EasyExcelUtil.onceExportByEntity("考试记录", response, list, FinishPagerRes.class, null, null);
    }
}
